package com.kanshu.ksgb.fastread.doudou.module.book.view;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.dl7.recycler.helper.RecyclerViewHelper;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.module.book.activity.SearchActivity;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.SearchRequestParams;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static com.kanshu.ksgb.fastread.doudou.base.baseui.SwipeBackActivity.ACTION_DESTORY;

/**
 * Created by wcy on 18/5/27.
 */

public class AdSearchVagueListLayout extends LinearLayout {
    @BindView(R.id.vague_list)
    RecyclerView mVagueList;
    private String mKeyWord;
    IItemClickCallback mCallback;
    VagueListAdapter mAdapter;
    List<BookInfo> mList = new ArrayList<>();
    public Subject<Integer> lifeCyclerSubject = PublishSubject.create();

    public void setCallback(IItemClickCallback callback) {
        mCallback = callback;
    }
    public AdSearchVagueListLayout(Context context) {
        super(context);
        init();
    }

    public AdSearchVagueListLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSearchVagueListLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_vague_list_layout, this);
        ButterKnife.bind(view);
        mAdapter = new VagueListAdapter(getContext(), mList);
        RecyclerViewHelper.initRecyclerViewV(getContext(), mVagueList, mAdapter);
        mAdapter.setOnItemClickListener((view1, position) -> {
            if (mCallback != null) {
                if (mList.size() != 0 && position < mList.size()) {
                    mCallback.onItemClcik(mList.get(position).book_title, mKeyWord);
                }
            }
        });
    }

    public void refreshByKeyword(String keyWord) {
        mKeyWord = keyWord;
        setBackgroundColor(Color.WHITE);
        SearchRequestParams params = new SearchRequestParams();
        params.book_title = keyWord;
        BookCityPresenter presenter = new BookCityPresenter(lifeCyclerSubject);
        mList.clear();
        mAdapter.notifyDataSetChanged();
        presenter.doSearch(params, new INetCommCallback<List<BookInfo>>() {
            @Override
            public void onResponse(List<BookInfo> list) {
                if (Utils.isEmptyList(list) || TextUtils.isEmpty(mKeyWord)) {
                    DisplayUtils.gone(AdSearchVagueListLayout.this);
                    return;
                }
                if(mCallback instanceof SearchActivity && !((SearchActivity) mCallback).isFinishing() && !((SearchActivity) mCallback).getFragmentContainerVisible()) {
                    DisplayUtils.visible(AdSearchVagueListLayout.this);
                    mList.clear();
                    mList.addAll(list);
                    mAdapter.notifyDataSetChanged();
                }
            }
            @Override
            public void onError(int code, String errDesc) {
                DisplayUtils.gone(AdSearchVagueListLayout.this);
            }
        });
    }
    class VagueListAdapter extends BaseQuickAdapter<BookInfo> {

        public VagueListAdapter(Context context, List<BookInfo> data) {
            super(context, data);
        }

        @Override
        protected int attachLayoutRes() {
            return R.layout.item_vague_layout;
        }

        @Override
        protected void convert(BaseViewHolder holder, BookInfo item, int position) {
            holder.setText(R.id.book_title, item.book_title);
            handleKeyWord(mKeyWord, holder, item);
        }


    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        lifeCyclerSubject.onNext(ACTION_DESTORY);
    }

    public static void handleKeyWord(String keyWord, BaseViewHolder holder, BookInfo item) {
        if (!TextUtils.isEmpty(keyWord)) {
            String bookTitle = item.book_title;
            int index = bookTitle.indexOf(keyWord);
            if (index != -1) {
                TextView tv = holder.getView(R.id.book_title);
                SpannableString cs = new SpannableString(bookTitle);
                int color = Xutils.getContext().getResources().getColor(R.color.vague_list_item_text_color);
                cs.setSpan(new ForegroundColorSpan(color), index, index + keyWord.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                tv.setText(cs);
            }
        }
    }

}
